﻿// -----------------------------------------------------------------------
// <copyright file="FileExtensions.cs" company="Abyssalsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Core.Config
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Xml;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class FileExtensions
    {
        /// <summary>Used to load a xml from file where our filters are stored</summary>
        private XmlDocument xmlDocument = new XmlDocument();

        /// <summary>Used to load a xml from file where our file associations are stored</summary>
        ////private XmlDocument fileAssociations = new XmlDocument();

        /// <summary>Node used for supported extensions list</summary>
        private XmlNode xmlExtensions;

        /// <summary>Node used for associated extensions list</summary>
        private XmlNode xmlAssociations = null;

        /// <summary>Stores the path to our xml</summary>
        private string documentPath;

        /// <summary>Stores a list with audio extensions, used for OpenDialog</summary>
        private string audioExtensions;

        /// <summary>Stores a list with video extensions, used for OpenDialog</summary>
        private string videoExtensions;

        /// <summary>Initializes a new instance of the FileExtensions class.</summary>
        public FileExtensions()
        {
            try
            {
                this.documentPath = Paths.UserFolder + "FileExtensions.xml";
                this.xmlDocument.Load(this.documentPath);
                ////this.fileAssociations.Load(Paths.UserFolder + "FileAssociations.xml");
            }
            catch
            {
                this.xmlDocument.LoadXml("<fileextension></fileextension>");
                ////this.fileAssociations.Load("<settings></settings>");
            }
        }

        /// <summary>Enumeration of file type</summary>
        public enum DataType
        {
            /// <summary>Is Audio</summary>
            audio,

            /// <summary>Is Video</summary>
            video,

            /// <summary>Is Radio</summary>
            radio
        }

        /// <summary>Gets the extensions</summary>
        public XmlNode XmlExtensions
        {
            get
            {
                return this.xmlExtensions;
            }
        }

        /// <summary>Gets the associations</summary>
        public XmlNode XmlAssociations
        {
            get
            {
                return this.xmlAssociations;
            }
        }

        /// <summary>Get all supported extensions of a given type</summary>
        /// <param name="type">Type of the files we want to get</param>
        /// <returns>Names extensions</returns>
        public int GetAllExtensions(DataType type)
        {
            this.xmlExtensions = this.xmlDocument.SelectSingleNode("fileextension/" + type.ToString());

            if (this.xmlExtensions != null)
            {
                return this.xmlExtensions.ChildNodes.Count;
            }
            else
            {
                return 0;
            }
        }

        /// <summary>Get specific extension</summary>
        /// <param name="extension">Type of the files we want to get</param>
        /// <returns>Returns description and icon on a given extension</returns>
        public string GetExtension(string extension)
        {
            string data;

            // Check if is audio file
            this.xmlExtensions = this.xmlDocument.SelectSingleNode("fileextension/audio/extension/" + extension);
            if (this.xmlExtensions != null)
            {
                data = this.xmlExtensions.InnerText + ";" + this.xmlExtensions.NextSibling.InnerText;
                return data;
            }
            else
            {
                // If it's not audio
                this.xmlExtensions = this.xmlDocument.SelectSingleNode("fileextension/video/extension/" + extension);
                if (this.xmlExtensions != null)
                {
                    data = this.xmlExtensions.InnerText + ";" + this.xmlExtensions.NextSibling.InnerText;
                    return data;
                }
                else
                {
                    // If it's not video
                    this.xmlExtensions = this.xmlDocument.SelectSingleNode("fileextension/radio/extension/" + extension);
                    if (this.xmlExtensions != null)
                    {
                        data = this.xmlExtensions.InnerText + ";" + this.xmlExtensions.NextSibling.InnerText;
                        return data;
                    }
                    else
                    {
                        // If it's not even radio... Then this extension is not supported
                        return string.Empty;
                    }
                }
            }
        }

        ///// <summary>Load supported file extensions</summary>
        ///// <param name="type">Specify the desired file types (audio, video)</param>
        ////public void LoadExtensions(DataType type)
        ////{
        ////    this.xmlAssociations = this.fileAssociations.SelectSingleNode("settings/" + type.ToString());
        ////}

        /////// <summary>Save to xml the list with extensions</summary>
        ////public void SaveExtensions()
        ////{
        ////    try
        ////    {
        ////        XmlTextWriter writer = new XmlTextWriter(Paths.UserFolder + "\\FileAssociations.xml", null);
        ////        writer.Formatting = Formatting.Indented;

        ////        this.fileAssociations.LoadXml("<settings> " + this.xmlAssociations.OuterXml + "</settings> ");
        ////        this.fileAssociations.Save(writer);
        ////    }
        ////    catch
        ////    {
        ////    }
        ////}

        /// <summary>This method builds the Filter for OpenFile dialog</summary>
        /// <returns>A string containing the supported extensions</returns>
        public string GetFilter()
        {
            string filter;
            string audioFilter;
            string videoFilter;
            string allSupported;
            string allFiles;

            this.audioExtensions = string.Empty;
            this.videoExtensions = string.Empty;

            XmlNode xmlFilter;

            // Select all audio extensions
            xmlFilter = this.xmlDocument.SelectSingleNode("fileextension/audio");
            foreach (XmlNode child in xmlFilter)
            {
                this.audioExtensions = this.audioExtensions + "*." + child.FirstChild.Name + ";";
            }

            audioFilter = "Audio files (" + this.audioExtensions + ")|" + this.audioExtensions + "|";

            // Select all video extensions
            xmlFilter = this.xmlDocument.SelectSingleNode("fileextension/video");
            foreach (XmlNode child in xmlFilter)
            {
                this.videoExtensions = this.videoExtensions + "*." + child.FirstChild.Name + ";";
            }

            videoFilter = "Video files (" + this.videoExtensions + ")|" + this.videoExtensions + "|";

            // Select all audio and video (All supported files)
            allSupported = "All supported files (" + this.audioExtensions + this.videoExtensions + ")|" + this.audioExtensions + this.videoExtensions + "|";

            // All files
            allFiles = "All files (*.*)|*.*";

            // We've build the filter, now concatenate the strings and return them;
            filter = audioFilter + videoFilter + allSupported + allFiles;
            return filter;
        }

        /// <summary>Check if specified extension is supported. Used to filter files</summary>
        /// <param name="ext">Extension to check</param>
        /// <returns>Returns true if this extension is supported</returns>
        public bool IsSupported(string ext)
        {
            if (this.audioExtensions == null)
            {
                this.BuildExtensionList();
            }

            // Check if the file has no extension
            if (string.IsNullOrEmpty(ext) || ext.Length < 2)
            {
                return false;
            }

            bool inListAudio = this.audioExtensions.Contains(ext.ToLower());
            bool inListVideo = this.videoExtensions.Contains(ext.ToLower());

            // If we found the extension return True, else return False
            if (inListAudio == true || inListVideo == true)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// Concatenate all supported extensions
        /// </summary>
        private void BuildExtensionList()
        {
            XmlNode xmlFilter;

            // Select all audio extensions
            xmlFilter = this.xmlDocument.SelectSingleNode("fileextension/audio");
            foreach (XmlNode child in xmlFilter)
            {
                this.audioExtensions = this.audioExtensions + "*." + child.FirstChild.Name + ";";
            }

            // Select all video extensions
            xmlFilter = this.xmlDocument.SelectSingleNode("fileextension/video");
            foreach (XmlNode child in xmlFilter)
            {
                this.videoExtensions = this.videoExtensions + "*." + child.FirstChild.Name + ";";
            }
        }
    }
}
